package com.cote.douhahou.pretloan.launcher;

import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.os.Build;
import android.webkit.WebView;

import com.cote.douhahou.pretloan.any.data.AkomapalendASBuilder;
import com.cote.douhahou.pretloan.any.data.AkomapalendASSycManager;
import com.cote.douhahou.pretloan.utils.AkomapaLendUtil;

import org.jetbrains.annotations.NotNull;


public class BaseContext extends Application {

    private static BaseContext Instance;

    @NotNull
    public static BaseContext instance() {
        return Instance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Instance = this;
        try {
            init();
            AkomapaLendUtil.akomapalendInit(getApplicationContext());
            init2();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void init() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            String processName = packageName(this);
            if (!AkomapaLendUtil.packageName().equals(processName)) {
                WebView.setDataDirectorySuffix(processName);
            }
        }
    }

    public void init2() {
        try {
            AkomapalendASBuilder.printLog(false);
            AkomapalendASSycManager.instance().init(this);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private String packageName(Context context) {
        if (context == null) return null;
        ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        if (manager != null) {
            for (ActivityManager.RunningAppProcessInfo processInfo : manager.getRunningAppProcesses()) {
                if (processInfo.pid == android.os.Process.myPid()) {
                    return processInfo.processName;
                }
            }
        }
        return "";
    }


}
